//
// Student 1 Name, ID.  SuJiani,20242996
// Student 2 Name, ID.
// Student 3 Name, ID.
//
// Library.cpp
#include "Library.h"
#include <algorithm>
#include <iostream>


void Library::addBook(const std::string& title, const std::string& author, int year, const std::string& isbn) 
{
    try 
    {
        std::unique_ptr<Book> newBook = std::make_unique<Book>(title, author, year, isbn);
        books.push_back(std::move(newBook));
    }
    catch (const std::invalid_argument& e) 
    {
        std::cerr << "Error adding book: " << e.what() << std::endl;
    }
}

void Library::displayBooks() const 
{
    for (const auto& book : books)
    {
        std::cout << "Title: " << book->getTitle() << std::endl;
        std::cout << "Author: " << book->getAuthor() << std::endl;
        std::cout << "Year of Publication: " << book->getYearOfPublication() << std::endl;
        std::cout << "ISBN: " << book->getISBN() << std::endl;
        std::cout << "-------------------------" << std::endl;
    }
}

void Library::sortByTitle()
{
    std::sort(books.begin(), books.end(), [](const std::unique_ptr<Book>& a, const std::unique_ptr<Book>& b) {
        return a->getTitle() < b->getTitle();
    });
}

void Library::sortByAuthor()
{
    std::sort(books.begin(), books.end(), [](const std::unique_ptr<Book>& a, const std::unique_ptr<Book>& b)
    {
        return a->getAuthor() < b->getAuthor();
    });
}

void Library::sortByYearOfPublication() {
    std::sort(books.begin(), books.end(), [](const std::unique_ptr<Book>& a, const std::unique_ptr<Book>& b) 
    {
        return a->getYearOfPublication() < b->getYearOfPublication();
    });
}

void Library::sortByISBN() {
    std::sort(books.begin(), books.end(), [](const std::unique_ptr<Book>& a, const std::unique_ptr<Book>& b) 
    {
        return a->getISBN() < b->getISBN();
    });
}

bool Library::searchBook(const std::string& query) const
{

    for (const auto& book : books) 
    {
        if (book->getTitle() == query || book->getISBN() == query)
        {
            return true;
        }
    }
    return false;
}